package com.ebc.eservice.redis;

import com.ebc.eservice.mapper.CardMessageMapper;
import com.ebc.eservice.mapper.InteractClientMapper;
import com.ebc.eservice.util.MyTimeUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author: Joey
 * @Description:
 * @date:2022/9/27 17:18
 */
@SpringBootTest
public class map {

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    MyTimeUtil myTimeUtil;
    @Autowired
    InteractClientMapper interactClientMapper;

    @Test
    void test3(){
//        interactClientMapper.updateInteractNum("123",1);
//        redisTemplate.opsForValue().set("name","joey");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }

    @Test
    void test1(){


//        HashMap<String, Object> map2;
//        List<String> timeList = myTimeUtil.getSevenDay();
//        for (int i = 1; i < 8; i++) { //插入最近7天数据 测试
//            map2 = new HashMap<>();
//            map2.put("visit", 1);
//            map2.put("saveCard", 1);
//            map2.put("addContacts", 1);
//            map2.put("reposeCard", 1);
//            map2.put("message",1);
//            redisTemplate.opsForHash().putAll("card:"+1+":date:"+timeList.get(i-1), map2);
//        }
        redisTemplate.opsForHash().increment("card:1:"+1, "visit", 30);
//        redisTemplate.opsForHash().increment("card:1:"+2, "visit", 30);

        Map<String,Object> map = redisTemplate.opsForHash().entries("card:1:1");
        int card = (int) map.get("visit");
        int card2 = (int) map.get("saveCard");
        int card3 = (int) map.get("addContacts");
        int card4 = (int) map.get("reposeCard");
        //new 对象  =>  列表 一个循环 结束
        System.out.println(card);
       //自增
//        for (int i = 1; i < 8; i++) {
//            redisTemplate.opsForHash().increment("card:1:date:"+timeList.get(i-1), "visit", 30);
//        }
//        List<Integer> list = new ArrayList<Integer>();
//        for (int i = 1; i <  timeList.size() ; i++) {
//            Map<String,Object> map = redisTemplate.opsForHash().entries("card:1:date:"+timeList.get(i-1));
//            int card = (int) map.get("visit");
//            list.add(card);
//        }
//        System.out.println(list);
//        int num = 0;
//        for (Integer integer : list) {
//            num = num + integer;
//        }
//        System.out.println("近7日访客："+num);

    }
    @Test
    void test2(){

        Date date = new Date();
        System.out.println(date);
        long dateTime = date.getTime();
        Timestamp oldday = new Timestamp(dateTime - (7 * 24*60*60*1000));
        Date date2 = new Date();
        date2 = oldday;
        System.out.println(date2);
//        String time = myTimeUtil.FormatTime(date);
//        System.out.println(time);

        List<String> twoTime = myTimeUtil.getTwoTime(date, date2);
        System.out.println(twoTime.get(0));
        System.out.println(twoTime.get(1));

    }

    @Test
    void test5(){
        MyTimeUtil timeUtil = new MyTimeUtil();

        List<String> sevenDay = timeUtil.getSevenDay();
        //        String today = timeUtil.getToDay();
//        for (int i = 0; i < sevenDay.size(); i++) {//sevenDay.get(i)
//            redisTemplate.opsForHash().increment("card:"+2+":date:09-26",
//                    "visit", 1);
//            //redis  客户数量记录
////            redisTemplate.opsForHash().increment("user:"+i+":date:"+sevenDay.get(i),
////                    "interacts", 1);
//        }
        //获取近7日总数 访客数
        List<Integer> list = new ArrayList<Integer>();
        for (int i = 0; i <  sevenDay.size() ; i++) {
            Map<String,Object> map = redisTemplate.opsForHash().entries("card:"+2+":date:"+sevenDay.get(i));
            int card = (int) map.get("visit");
            list.add(card);
        }
        int num = 0;
        List<Integer> list2 = new ArrayList<Integer>();
        for (Integer integer : list) {
            num = num + integer;
            list2.add(integer);
        }
        System.out.println("日期=> "+sevenDay);
        System.out.println("近7日总访客："+num);
        System.out.println("每天"+list2);
    }

    @Autowired
    private CardMessageMapper cardMessageMapper;
    //HyperLogLog
    @Test
    void test6(){
//        HyperLogLogOperations<String, String> clientVo = redisTemplate.opsForHyperLogLog();
//        MyTimeUtil timeUtil = new MyTimeUtil();
//        List<String> sevenDay = timeUtil.getSevenDay();
//        for (int i = 0; i < sevenDay.size(); i++) { //插入最近7天数据 测试
//            clientVo.add("card:2:date:"+sevenDay.get(i), "a","index:"+(i));
//        }
//
//        List<Long> clientList = new ArrayList<>();
//        for (int i = 0; i < sevenDay.size(); i++) {
//            clientList.add(clientVo.size("card:"+2+":date:"+sevenDay.get(i)));
//            clientVo.union("total", "card:"+2+":date:"+sevenDay.get(i));
//        }
//        System.out.println("今日："+clientVo.size("card:2:date:2022-09-28"));
//        System.out.println(clientList);
//        System.out.println("总数："+clientVo.size("total"));
        int todayMessage = cardMessageMapper.todayMessage();
        System.out.println(todayMessage);
    }


    @Test
    void test7(){//根据2个时间获取日期列表
        Date date = new Date();
        System.out.println(date);
        long dateTime = date.getTime();
        Timestamp oldday = new Timestamp(dateTime - (7 * 24*60*60*1000));
        Date date2 = new Date();
        date2 = oldday;

        int dist = myTimeUtil.getDateDist(date2, date);//begin  end
        System.out.println(dist);

        List<String> dateList = myTimeUtil.getAppointDateList(date, dist);
        System.out.println(dateList);


//获取2个日期 转换成天数  获取对应日期列表
//        redisTemplate.opsForHash().increment("card:"+cardId+":date:"+today,
//                "visit", 1);
    }

    @Test
    void tes8() throws InterruptedException {
        //获取时间 存入redis  stay:id 时间单位
        //退出时间 获取redis 并删除redis 计入 hashMap 时长
        long today = System.currentTimeMillis();
        Timestamp nowday = new Timestamp(today);

        Thread.sleep(10000);

        long today2 = System.currentTimeMillis();
        Timestamp oldday = new Timestamp(today2);
        long t1 = nowday.getTime();
        long t2 = oldday.getTime();//(1000*60*60*24)

    }

}
//  redisTemplate.opsForHash().delete("card:1:date:2022-9-20","visit");
//        redisTemplate.opsForHash().delete("card:1:date:2022-9-20","saveCard");
//        redisTemplate.opsForHash().delete("card:1:date:2022-9-20","addContacts");
//        redisTemplate.opsForHash().delete("card:1:date:2022-9-20","reposeCard");
//        redisTemplate.opsForHash().delete("card:1:date:2022-9-20","message");